package com.zqwh.xtool.api.offline;

import android.content.Context;

import com.zqwh.yj.sdk.utils.LogUtils;
import com.zqwh.xtool.greendao.DaoManager;
import com.zqwh.xtool.greendao.LendOrderDao;
import com.zqwh.xtool.greendao.db.LendOrder;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

public class LendOrderResposity {
    private DaoManager daoManager;
    private LendOrderDao lendOrderDao;

    public LendOrderResposity(Context context){
        daoManager = DaoManager.getInstance();
        daoManager.init(context);
        lendOrderDao = daoManager.getDaoSession().getLendOrderDao();
    }

    /**
     * 插入记录
     *
     * @param lendOrder
     * @return
     */
    public boolean insertLendOrder(LendOrder lendOrder) {
        return lendOrderDao.insertOrReplace(lendOrder) != -1;
    }

    /**
     * 插入多条记录
     * @param lendOrderList
     * @return
     */
    public boolean insertMultLendOrder(final List<LendOrder> lendOrderList){
        boolean flag = false;
        try {
            daoManager.getDaoSession().runInTx(new Runnable() {
                @Override
                public void run() {
                    for (LendOrder stock : lendOrderList) {
                        lendOrderDao.insertOrReplace(stock);
                    }
                }
            });
            flag = true;
        } catch (Exception e) {
            LogUtils.error("insertMultLendOrder failed , "+e.getMessage());
            e.printStackTrace();
        }
        return flag;
    }


    /**
     * 根据ID查询用户
     * @param lendOrderId
     * @return
     */
    public LendOrder queryLendOrderById(String lendOrderId){
        QueryBuilder<LendOrder> qb = lendOrderDao.queryBuilder();
        qb.where(LendOrderDao.Properties.LendOrderId.eq(lendOrderId));
        return qb.unique();
    }

    /**
     * 查询更新过的数据
     * @return
     */
    public List<LendOrder> queryLendOrderUpdated(){
        QueryBuilder<LendOrder> qb = lendOrderDao.queryBuilder();
        qb.where(LendOrderDao.Properties.Updated.eq(true));
        return qb.list();
    }

    /**
     * 删除所有数据
     */
    public void deleteAll(){
        lendOrderDao.deleteAll();
    }
}
